package com.clms.marketings.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clms.common.R;
import com.clms.marketings.mapper.SmsAdvertisePositionMapper;
import com.clms.marketings.mapper.TbAdvertiseMapper;
import com.clms.marketings.pojo.entity.SmsAdvertisePosition;
import com.clms.marketings.pojo.entity.TbAdvertise;
import com.clms.marketings.pojo.vo.*;
import com.clms.marketings.service.TbAdvertiseService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: clms-third
 * @description: 广告管理的业务逻辑层
 * @author: 赵露露
 * @create: 2021-07-23 16:48
 **/
@Service
public class TbAdvertiseServiceImpl implements TbAdvertiseService {
    @Autowired
    TbAdvertiseMapper tbAdvertiseMapper;
    @Autowired
    SmsAdvertisePositionMapper smsAdvertisePositionMapper;
//    查询分页
    @Override
    public R selectAll(Integer currentPage, Integer pageSize, AdvertiseVo advertiseVo) {
        Page<TbAdvertise> page=new Page<>(currentPage,pageSize);
        QueryWrapper<TbAdvertise> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotEmpty(advertiseVo.getName())){
            queryWrapper.like("name",advertiseVo.getName());
        }
        IPage<TbAdvertise> adIpage = tbAdvertiseMapper.selectAll(page, queryWrapper);
        System.out.println(adIpage.getRecords());
        return R.ok().put("total",adIpage.getTotal()).put("records",adIpage.getRecords());
    }
//增加广告管理
    @Override
    public R insertAdvertise(TbAdvertise tbAdvertise) {
        int insertAdvertise= tbAdvertiseMapper.insert(tbAdvertise);
        return R.ok();
    }
    //修改广告管理
    @Override
    public R updateAdvertise(TbAdvertise tbAdvertise) {
        int updateAdvertise= tbAdvertiseMapper.updateById(tbAdvertise);
        return R.ok();
    }
    //删除广告管理
    @Override
    public R deleteAdvertise(Integer id) {
        int deleteAdvertise=tbAdvertiseMapper.deleteById(id);
        return R.ok();
    }
    //回显广告管理
    @Override
    public R selectById(Integer id) {
        TbAdvertise tbAdvertise=tbAdvertiseMapper.selectById(id);
        return R.ok().put("previews",tbAdvertise);
    }
    //名字唯一性校验
    @Override
    public R nameUnique(AdvertiseVoVo advertiseVoVo) {
        QueryWrapper wrapper=new QueryWrapper();
        if (StringUtils.isNotEmpty(advertiseVoVo.getName())){
            wrapper.eq("name",advertiseVoVo.getName());
        }
        if (StringUtils.isNotEmpty(advertiseVoVo.getId())){
            wrapper.ne("id",advertiseVoVo.getId());
        }
        if (StringUtils.isNotEmpty(advertiseVoVo.getName())){
            TbAdvertise tbAdvertise = tbAdvertiseMapper.selectOne(wrapper);
            System.out.println(tbAdvertise!=null);
            if (tbAdvertise!=null){
                return R.error().message("广告存在");
            }else {
                return R.ok().message("可以添加");
            }
        }else {
            return R.error().message("不能为空");
        }

    }
    //查询轮播图
    @Override
    public R selectImgs(AdvertiseVoVo advertiseVoVo) {
        QueryWrapper wrapper=new QueryWrapper();
        if (StringUtils.isNotEmpty(advertiseVoVo.getName())){
            wrapper.eq("name",advertiseVoVo.getName());
        }
        if (StringUtils.isNotEmpty(advertiseVoVo.getPic())) {
            wrapper.eq("pic",advertiseVoVo.getPic());
        }
        if (StringUtils.isNotEmpty(advertiseVoVo.getId())){
            wrapper.ne("id",advertiseVoVo.getId());
        }
        if (StringUtils.isNotEmpty(advertiseVoVo.getPic())){
            TbAdvertise tbAdvertise = tbAdvertiseMapper.selectOne(wrapper);
            String tbAdvertise1=advertiseVoVo.getName();
        }
       return R.ok().put("allImg",advertiseVoVo.getPic()).put("allName",advertiseVoVo.getName());
    }



}
